﻿Imports System.Drawing.Printing

Public Class Printingimage
    Shared prntdoc As New PrintDocument
    Shared New_Bitmap As Bitmap
    ''' <summary>
    ''' The PictureBox with the image to print loaded.
    ''' </summary>
    ''' <param name="originalpicturebox"></param>
    Public Sub printimage(originalpicturebox As PictureBox, prntdoc As PrintDocument)
        Try
            Dim picturebox2 As PictureBox

            Dim w As Integer
            Dim h As Integer
            Dim str As String
            picturebox2.Image = originalpicturebox.Image
            If picturebox2.Image.Width > picturebox2.Image.Height Then
                picturebox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
            End If
            str = "1000"
            Dim Height As Double = Convert.ToDouble(str)
            Dim width As Double

            Dim aspectRatio As Double
            aspectRatio = picturebox2.Image.Width / picturebox2.Image.Height
            width = Convert.ToDouble(str * aspectRatio)
            If width > 850 Then
                picturebox2.Image = originalpicturebox.Image
                originalpicturebox.Visible = False
                If picturebox2.Image.Width > picturebox2.Image.Height Then
                    picturebox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
                End If
                str = "900"

                h = picturebox2.Image.Size.Height
                w = picturebox2.Image.Size.Width

                Height = Convert.ToDouble(str)

                aspectRatio = picturebox2.Image.Width / picturebox2.Image.Height
                width = Convert.ToDouble(str * aspectRatio)
                If width > 891 Then
                    picturebox2.Image = originalpicturebox.Image
                    originalpicturebox.Visible = False
                    If picturebox2.Image.Width > picturebox2.Image.Height Then
                        picturebox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
                    End If
                    str = "700"

                    Height = Convert.ToDouble(str)
                    width = Convert.ToDouble(str * aspectRatio)
                Else
                    Dim wi As Integer
                    wi = Convert.ToInt32(width)
                    Dim hi As Integer
                    hi = Convert.ToInt32(Height)
                    Dim New_Bitmap As New Bitmap(picturebox2.Image, wi, hi)
                    picturebox2.Image = New_Bitmap
                    w = width
                End If
            Else
                Dim wi As Integer
                wi = Convert.ToInt32(width)
                Dim hi As Integer
                hi = Convert.ToInt32(Height)
                New_Bitmap = New Bitmap(picturebox2.Image, wi, hi)
                picturebox2.Image = New_Bitmap
                w = width

            End If
            '    End If
            prntdoc.Print()
        Catch ex As Exception
            Throw New Exception("Error printing image.")
        End Try

    End Sub
    ''' <summary>
    ''' This sub needs to be called by the sub "yourprintdocument_print" (the one you referenced at printimage sub). Put here the "e" parameter of that sub."
    ''' </summary>
    ''' <param name="e"></param>
    Public Shared Sub FinalPrint(e As PrintPageEventArgs)
        e.Graphics.DrawImage(New_Bitmap, 0, 0)
    End Sub
End Class
